package com.feicheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.base.common.DaoConstant;
import com.feicheng.base.result.Result;
import com.feicheng.model.StockAndProduct;
import com.feicheng.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao.zhang
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/stock")
public class StockController {
@Autowired
    IStockService iStockService;

    /**
     * 库存列表条件分页查询接口
     * @param current 当前页
     * @param size 每页条数
     * @param stockAndProduct 业务参数（查询条件）
     * @author tao.jiang
     * @return
     */
    @PostMapping("/page/g")
    public Result selectUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody StockAndProduct stockAndProduct ){
        System.out.println(stockAndProduct);
        // 构建查询条件
        QueryWrapper<StockAndProduct> stockControllerQueryWrapper = new QueryWrapper<StockAndProduct>();
        // 如果搜索仓库不是空把仓库加入查询的条件中
        if (StringUtils.hasLength(stockAndProduct.getWarehouseName())) {
            stockControllerQueryWrapper.like(DaoConstant.STOCKANDPRODUCT.WAREHOUSE_NAME, stockAndProduct.getWarehouseName());
        }
        //  如果搜索产品名称不是空把产品名称加入到查询的条件中
        if (StringUtils.hasLength(stockAndProduct.getProductName())) {
            stockControllerQueryWrapper.like(DaoConstant.STOCKANDPRODUCT.PRODUCT_NAME,stockAndProduct.getProductName());
        }
        // 如果仓库管理员不是空把仓库管理加入到查询条件中
        if (StringUtils.hasLength(stockAndProduct.getUserName())) {
            stockControllerQueryWrapper.like(DaoConstant.STOCKANDPRODUCT.USER_NAME,stockAndProduct.getUserName());
        }

        // 创建分页查询
        Page<StockAndProduct> pageInfo = new Page<>(current, size);
        // 返回查询结果
        return Result.success(iStockService.getPotionPages(pageInfo,stockControllerQueryWrapper));

    }

}

